package com.gange.controller;

import com.gange.pojo.Emp;
import com.gange.pojo.PageBean;
import com.gange.pojo.Result;
import com.gange.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@RequestMapping("/emps")
@RestController
@Slf4j
public class EmpController {
    @Autowired
    private EmpService es;
    @GetMapping
    public Result page(
            @RequestParam(required = false) String name,
            @RequestParam(required = false) Short gender,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end,
            @RequestParam(required = false,defaultValue = "1") Integer page,
            Integer pageSize
    ){
        PageBean pageBean = es.page(page, pageSize,name,gender,begin,end);
        return Result.success(pageBean);
    }

    @DeleteMapping("/{ids}")
    public Result deletes(@PathVariable("ids") List<Integer> ids){
        int deletes = es.deletes(ids);
        log.info("删除了{}条数据",deletes);
        return Result.success();
    }
    @PostMapping
    public Result save(@RequestBody Emp emp){
        //记录日志
        log.info("新增员工, emp:{}",emp);
        //调用业务层新增功能
        es.save(emp);
        //响应
        return Result.success();
    }
}
